<!DOCTYPE html>
<html lang="zh" dir="ltr" class="client-nojs">
<head>
<title>std::async</title>
<meta charset="UTF-8">
<meta name="generator" content="MediaWiki 1.21.2">
<link rel="shortcut icon" href="../../../common/favicon.ico">
<link rel="stylesheet" href="../../../common/ext.css">
<meta name="ResourceLoaderDynamicStyles" content="">
<link rel="stylesheet" href="../../../common/site_modules.css">
<style>a:lang(ar),a:lang(ckb),a:lang(fa),a:lang(kk-arab),a:lang(mzn),a:lang(ps),a:lang(ur){text-decoration:none}#toc{display:none}.editsection{display:none}
/* cache key: mwiki1-mwiki_zh_:resourceloader:filter:minify-css:7:15cea3ec788a65b5187d4018eed543bf */</style>

<script src="../../../common/startup_scripts.js"></script>
<script>if(window.mw){
mw.config.set({"wgCanonicalNamespace":"","wgCanonicalSpecialPageName":false,"wgNamespaceNumber":0,"wgPageName":"cpp/thread/async","wgTitle":"cpp/thread/async","wgCurRevisionId":65236,"wgArticleId":3740,"wgIsArticle":true,"wgAction":"view","wgUserName":null,"wgUserGroups":["*"],"wgCategories":[],"wgBreakFrames":false,"wgPageContentLanguage":"zh","wgSeparatorTransformTable":["",""],"wgDigitTransformTable":["",""],"wgDefaultDateFormat":"zh","wgMonthNames":["","1月","2月","3月","4月","5月","6月","7月","8月","9月","10月","11月","12月"],"wgMonthNamesShort":["","1月","2月","3月","4月","5月","6月","7月","8月","9月","10月","11月","12月"],"wgRelevantPageName":"cpp/thread/async","wgUserVariant":"zh","wgRestrictionEdit":[],"wgRestrictionMove":[]});
}</script><script>if(window.mw){
mw.loader.implement("user.options",function(){mw.user.options.set({"ccmeonemails":0,"cols":80,"date":"default","diffonly":0,"disablemail":0,"disablesuggest":0,"editfont":"default","editondblclick":0,"editsection":0,"editsectiononrightclick":0,"enotifminoredits":0,"enotifrevealaddr":0,"enotifusertalkpages":1,"enotifwatchlistpages":0,"extendwatchlist":0,"externaldiff":0,"externaleditor":0,"fancysig":0,"forceeditsummary":0,"gender":"unknown","hideminor":0,"hidepatrolled":0,"imagesize":2,"justify":0,"math":1,"minordefault":0,"newpageshidepatrolled":0,"nocache":0,"noconvertlink":0,"norollbackdiff":0,"numberheadings":0,"previewonfirst":0,"previewontop":1,"quickbar":5,"rcdays":7,"rclimit":50,"rememberpassword":0,"rows":25,"searchlimit":20,"showhiddencats":0,"showjumplinks":1,"shownumberswatching":1,"showtoc":0,"showtoolbar":1,"skin":"cppreference2","stubthreshold":0,"thumbsize":2,"underline":2,"uselivepreview":0,"usenewrc":0,"watchcreations":0,"watchdefault":0,"watchdeletion":0,
"watchlistdays":3,"watchlisthideanons":0,"watchlisthidebots":0,"watchlisthideliu":0,"watchlisthideminor":0,"watchlisthideown":0,"watchlisthidepatrolled":0,"watchmoves":0,"wllimit":250,"variant":"zh","language":"zh","searchNs0":true,"searchNs1":false,"searchNs2":false,"searchNs3":false,"searchNs4":false,"searchNs5":false,"searchNs6":false,"searchNs7":false,"searchNs8":false,"searchNs9":false,"searchNs10":false,"searchNs11":false,"searchNs12":false,"searchNs13":false,"searchNs14":false,"searchNs15":false,"gadget-MathJax":1,"gadget-ColiruCompiler":1});;},{},{});mw.loader.implement("user.tokens",function(){mw.user.tokens.set({"editToken":"+\\","patrolToken":false,"watchToken":false});;},{},{});
/* cache key: mwiki1-mwiki_zh_:resourceloader:filter:minify-js:7:258d7cd6aa9aa67dee25e01fb6a9e505 */
}</script>
<script>if(window.mw){
mw.loader.load(["mediawiki.page.startup","mediawiki.legacy.wikibits","mediawiki.legacy.ajax"]);
}</script>
<style type="text/css">/*<![CDATA[*/
.source-cpp {line-height: normal;}
.source-cpp li, .source-cpp pre {
	line-height: normal; border: 0px none white;
}
/**
 * GeSHi Dynamically Generated Stylesheet
 * --------------------------------------
 * Dynamically generated stylesheet for cpp
 * CSS class: source-cpp, CSS id: 
 * GeSHi (C) 2004 - 2007 Nigel McNie, 2007 - 2008 Benny Baumann
 * (http://qbnz.com/highlighter/ and http://geshi.org/)
 * --------------------------------------
 */
.cpp.source-cpp .de1, .cpp.source-cpp .de2 {font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;}
.cpp.source-cpp  {font-family:monospace;}
.cpp.source-cpp .imp {font-weight: bold; color: red;}
.cpp.source-cpp li, .cpp.source-cpp .li1 {font-weight: normal; vertical-align:top;}
.cpp.source-cpp .ln {width:1px;text-align:right;margin:0;padding:0 2px;vertical-align:top;}
.cpp.source-cpp .li2 {font-weight: bold; vertical-align:top;}
.cpp.source-cpp .kw1 {color: #0000dd;}
.cpp.source-cpp .kw2 {color: #0000ff;}
.cpp.source-cpp .kw3 {color: #0000dd;}
.cpp.source-cpp .kw4 {color: #0000ff;}
.cpp.source-cpp .co1 {color: #909090;}
.cpp.source-cpp .co2 {color: #339900;}
.cpp.source-cpp .coMULTI {color: #ff0000; font-style: italic;}
.cpp.source-cpp .es0 {color: #008000; font-weight: bold;}
.cpp.source-cpp .es1 {color: #008000; font-weight: bold;}
.cpp.source-cpp .es2 {color: #008000; font-weight: bold;}
.cpp.source-cpp .es3 {color: #008000; font-weight: bold;}
.cpp.source-cpp .es4 {color: #008000; font-weight: bold;}
.cpp.source-cpp .es5 {color: #008000; font-weight: bold;}
.cpp.source-cpp .br0 {color: #008000;}
.cpp.source-cpp .sy0 {color: #008000;}
.cpp.source-cpp .sy1 {color: #000080;}
.cpp.source-cpp .sy2 {color: #000040;}
.cpp.source-cpp .sy3 {color: #000040;}
.cpp.source-cpp .sy4 {color: #008080;}
.cpp.source-cpp .st0 {color: #008000;}
.cpp.source-cpp .nu0 {color: #000080;}
.cpp.source-cpp .nu6 {color: #000080;}
.cpp.source-cpp .nu8 {color: #000080;}
.cpp.source-cpp .nu12 {color: #000080;}
.cpp.source-cpp .nu16 {color:#000080;}
.cpp.source-cpp .nu17 {color:#000080;}
.cpp.source-cpp .nu18 {color:#000080;}
.cpp.source-cpp .nu19 {color:#000080;}
.cpp.source-cpp .ln-xtra, .cpp.source-cpp li.ln-xtra, .cpp.source-cpp div.ln-xtra {background-color: #ffc;}
.cpp.source-cpp span.xtra { display:block; }

/*]]>*/
</style><style type="text/css">/*<![CDATA[*/
.source-text {line-height: normal;}
.source-text li, .source-text pre {
	line-height: normal; border: 0px none white;
}
/**
 * GeSHi Dynamically Generated Stylesheet
 * --------------------------------------
 * Dynamically generated stylesheet for text
 * CSS class: source-text, CSS id: 
 * GeSHi (C) 2004 - 2007 Nigel McNie, 2007 - 2008 Benny Baumann
 * (http://qbnz.com/highlighter/ and http://geshi.org/)
 * --------------------------------------
 */
.text.source-text .de1, .text.source-text .de2 {font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;}
.text.source-text  {font-family:monospace;}
.text.source-text .imp {font-weight: bold; color: red;}
.text.source-text li, .text.source-text .li1 {font-weight: normal; vertical-align:top;}
.text.source-text .ln {width:1px;text-align:right;margin:0;padding:0 2px;vertical-align:top;}
.text.source-text .li2 {font-weight: bold; vertical-align:top;}
.text.source-text .ln-xtra, .text.source-text li.ln-xtra, .text.source-text div.ln-xtra {background-color: #ffc;}
.text.source-text span.xtra { display:block; }

/*]]>*/
</style><!--[if lt IE 7]><style type="text/css">body{behavior:url("/mwiki/skins/cppreference2/csshover.min.htc")}</style><![endif]--></head>
<body class="mediawiki ltr sitedir-ltr ns-0 ns-subject page-cpp_thread_async skin-cppreference2 action-view cpp-navbar">
        <!-- header -->
        <!-- /header -->
        <!-- content -->
<div id="cpp-content-base">
            <div id="content">
                <a id="top"></a>
                <div id="mw-js-message" style="display:none;"></div>
                                <!-- firstHeading -->
<h1 id="firstHeading" class="firstHeading"><span style="font-size:0.7em; line-height:130%">std::</span>async</h1>
                <!-- /firstHeading -->
                <!-- bodyContent -->
                <div id="bodyContent">
                                        <!-- tagline -->
                    <div id="siteSub">来自cppreference.com</div>
                    <!-- /tagline -->
                                        <!-- subtitle -->
                    <div id="contentSub"><span class="subpages">&lt; <a href="../../cpp.html" title="cpp">cpp</a>‎ | <a href="../thread.html" title="cpp/thread">thread</a></span></div>
                    <!-- /subtitle -->
                                                            <!-- bodycontent -->
                    <div id="mw-content-text" lang="zh" dir="ltr" class="mw-content-ltr"><div class="t-navbar" style=""><div class="t-navbar-sep"> </div><div class="t-navbar-head"><a href="../../cpp.html" title="cpp"> C++</a><div class="t-navbar-menu"><div><div><table class="t-nv-begin" cellpadding="0" style="line-height:1.1em;">
<tr class="t-nv"><td colspan="5"> <a href="../language.html" title="cpp/language">语言</a> </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="../header.html" title="cpp/header">标准库头文件</a> </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="../freestanding.html" title="cpp/freestanding"> 自立与有宿主实现</a> </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="../named_req.html" title="cpp/named req">具名要求</a> </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="../utility.html#.E8.AF.AD.E8.A8.80.E6.94.AF.E6.8C.81" title="cpp/utility">语言支持库</a> </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="../concepts.html" title="cpp/concepts">概念库</a> <span class="t-mark-rev t-since-cxx20">(C++20)</span> </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="../error.html" title="cpp/error">诊断库</a> </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="../utility.html" title="cpp/utility">工具库</a> </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="../string.html" title="cpp/string">字符串库</a> </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="https://zh.cppreference.com/w/cpp/container" title="cpp/container">容器库</a> </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="../iterator.html" title="cpp/iterator">迭代器库</a> </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="../ranges.html" title="cpp/ranges"> 范围库</a> <span class="t-mark-rev t-since-cxx20">(C++20)</span> </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="../algorithm.html" title="cpp/algorithm">算法库</a> </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="../numeric.html" title="cpp/numeric">数值库</a> </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="../io.html" title="cpp/io">输入/输出库</a> </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="../locale.html" title="cpp/locale">本地化库</a> </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="../regex.html" title="cpp/regex">正则表达式库</a> <span class="t-mark-rev t-since-cxx11">(C++11)</span> </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="../atomic.html" title="cpp/atomic">原子操作库</a> <span class="t-mark-rev t-since-cxx11">(C++11)</span> </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="../thread.html" title="cpp/thread">线程支持库</a> <span class="t-mark-rev t-since-cxx11">(C++11)</span> </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="../filesystem.html" title="cpp/filesystem">文件系统库</a> <span class="t-mark-rev t-since-cxx17">(C++17)</span> </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="../experimental.html" title="cpp/experimental">技术规范</a> </td></tr>
</table></div><div></div></div></div></div><div class="t-navbar-sep"> </div><div class="t-navbar-head"><a href="../thread.html" title="cpp/thread">线程支持库</a><div class="t-navbar-menu"><div><div><table class="t-nv-begin" cellpadding="0" style="">
<tr class="t-nv-h1"><td colspan="5"> 线程</td></tr>
<tr class="t-nv"><td colspan="5"><div class="t-nv-ln-table"><div><a href="thread.html" title="cpp/thread/thread"><span class="t-lines"><span>thread</span></span></a></div><div><span class="t-lines"><span><span class="t-mark-rev t-since-cxx11">(C++11)</span></span></span></div></div></td></tr>
<tr class="t-nv"><td colspan="5"><div class="t-nv-ln-table"><div><a href="jthread.html" title="cpp/thread/jthread"><span class="t-lines"><span>jthread</span></span></a></div><div><span class="t-lines"><span><span class="t-mark-rev t-since-cxx20">(C++20)</span></span></span></div></div></td></tr>
<tr class="t-nv"><td colspan="5"><div class="t-nv-ln-table"><div><a href="stop_token.html" title="cpp/thread/stop token"><span class="t-lines"><span>stop_token</span></span></a></div><div><span class="t-lines"><span><span class="t-mark-rev t-since-cxx20">(C++20)</span></span></span></div></div></td></tr>
<tr class="t-nv"><td colspan="5"><div class="t-nv-ln-table"><div><a href="stop_source.html" title="cpp/thread/stop source"><span class="t-lines"><span>stop_source</span></span></a></div><div><span class="t-lines"><span><span class="t-mark-rev t-since-cxx20">(C++20)</span></span></span></div></div></td></tr>
<tr class="t-nv"><td colspan="5"><div class="t-nv-ln-table"><div><a href="stop_callback.html" title="cpp/thread/stop callback"><span class="t-lines"><span>stop_callback</span></span></a></div><div><span class="t-lines"><span><span class="t-mark-rev t-since-cxx20">(C++20)</span></span></span></div></div></td></tr>
<tr class="t-nv"><td colspan="5"><div class="t-nv-ln-table"><div><a href="hardware_destructive_interference_size.html" title="cpp/thread/hardware destructive interference size"><span class="t-lines"><span>hardware_destructive_interference_size</span><span>hardware_constructive_interference_size</span></span></a></div><div><span class="t-lines"><span><span class="t-mark-rev t-since-cxx17">(C++17)</span></span><span><span class="t-mark-rev t-since-cxx17">(C++17)</span></span></span></div></div></td></tr>
<tr class="t-nv-h2"><td colspan="5"> <code>this_thread</code> 命名空间</td></tr>
<tr class="t-nv-col-table"><td><div><table class="t-nv-begin" cellpadding="0" style="">
<tr class="t-nv"><td colspan="5"><div class="t-nv-ln-table"><div><a href="get_id.html" title="cpp/thread/get id"><span class="t-lines"><span>get_id</span></span></a></div><div><span class="t-lines"><span><span class="t-mark-rev t-since-cxx11">(C++11)</span></span></span></div></div></td></tr>
<tr class="t-nv"><td colspan="5"><div class="t-nv-ln-table"><div><a href="yield.html" title="cpp/thread/yield"><span class="t-lines"><span>yield</span></span></a></div><div><span class="t-lines"><span><span class="t-mark-rev t-since-cxx11">(C++11)</span></span></span></div></div></td></tr>
</table></div></td><td><div><table class="t-nv-begin" cellpadding="0" style="">
<tr class="t-nv"><td colspan="5"><div class="t-nv-ln-table"><div><a href="sleep_for.html" title="cpp/thread/sleep for"><span class="t-lines"><span>sleep_for</span></span></a></div><div><span class="t-lines"><span><span class="t-mark-rev t-since-cxx11">(C++11)</span></span></span></div></div></td></tr>
<tr class="t-nv"><td colspan="5"><div class="t-nv-ln-table"><div><a href="sleep_until.html" title="cpp/thread/sleep until"><span class="t-lines"><span>sleep_until</span></span></a></div><div><span class="t-lines"><span><span class="t-mark-rev t-since-cxx11">(C++11)</span></span></span></div></div></td></tr>
</table></div></td></tr>
<tr class="t-nv-h1"><td colspan="5"> 互斥</td></tr>
<tr class="t-nv-col-table"><td><div><table class="t-nv-begin" cellpadding="0" style="">
<tr class="t-nv"><td colspan="5"><div class="t-nv-ln-table"><div><a href="mutex.html" title="cpp/thread/mutex"><span class="t-lines"><span>mutex</span></span></a></div><div><span class="t-lines"><span><span class="t-mark-rev t-since-cxx11">(C++11)</span></span></span></div></div></td></tr>
<tr class="t-nv"><td colspan="5"><div class="t-nv-ln-table"><div><a href="recursive_mutex.html" title="cpp/thread/recursive mutex"><span class="t-lines"><span>recursive_mutex</span></span></a></div><div><span class="t-lines"><span><span class="t-mark-rev t-since-cxx11">(C++11)</span></span></span></div></div></td></tr>
<tr class="t-nv"><td colspan="5"><div class="t-nv-ln-table"><div><a href="shared_mutex.html" title="cpp/thread/shared mutex"><span class="t-lines"><span>shared_mutex</span></span></a></div><div><span class="t-lines"><span><span class="t-mark-rev t-since-cxx17">(C++17)</span></span></span></div></div></td></tr>
</table></div></td><td><div><table class="t-nv-begin" cellpadding="0" style="">
<tr class="t-nv"><td colspan="5"><div class="t-nv-ln-table"><div><a href="timed_mutex.html" title="cpp/thread/timed mutex"><span class="t-lines"><span>timed_mutex</span></span></a></div><div><span class="t-lines"><span><span class="t-mark-rev t-since-cxx11">(C++11)</span></span></span></div></div></td></tr>
<tr class="t-nv"><td colspan="5"><div class="t-nv-ln-table"><div><a href="recursive_timed_mutex.html" title="cpp/thread/recursive timed mutex"><span class="t-lines"><span>recursive_timed_mutex</span></span></a></div><div><span class="t-lines"><span><span class="t-mark-rev t-since-cxx11">(C++11)</span></span></span></div></div></td></tr>
<tr class="t-nv"><td colspan="5"><div class="t-nv-ln-table"><div><a href="shared_timed_mutex.html" title="cpp/thread/shared timed mutex"><span class="t-lines"><span>shared_timed_mutex</span></span></a></div><div><span class="t-lines"><span><span class="t-mark-rev t-since-cxx14">(C++14)</span></span></span></div></div></td></tr>
</table></div></td></tr>
<tr class="t-nv-h2"><td colspan="5"> 通用锁管理</td></tr>
<tr class="t-nv-col-table"><td><div><table class="t-nv-begin" cellpadding="0" style="">
<tr class="t-nv"><td colspan="5"><div class="t-nv-ln-table"><div><a href="lock_guard.html" title="cpp/thread/lock guard"><span class="t-lines"><span>lock_guard</span></span></a></div><div><span class="t-lines"><span><span class="t-mark-rev t-since-cxx11">(C++11)</span></span></span></div></div></td></tr>
<tr class="t-nv"><td colspan="5"><div class="t-nv-ln-table"><div><a href="scoped_lock.html" title="cpp/thread/scoped lock"><span class="t-lines"><span>scoped_lock</span></span></a></div><div><span class="t-lines"><span><span class="t-mark-rev t-since-cxx17">(C++17)</span></span></span></div></div></td></tr>
<tr class="t-nv"><td colspan="5"><div class="t-nv-ln-table"><div><a href="unique_lock.html" title="cpp/thread/unique lock"><span class="t-lines"><span>unique_lock</span></span></a></div><div><span class="t-lines"><span><span class="t-mark-rev t-since-cxx11">(C++11)</span></span></span></div></div></td></tr>
<tr class="t-nv"><td colspan="5"><div class="t-nv-ln-table"><div><a href="shared_lock.html" title="cpp/thread/shared lock"><span class="t-lines"><span>shared_lock</span></span></a></div><div><span class="t-lines"><span><span class="t-mark-rev t-since-cxx14">(C++14)</span></span></span></div></div></td></tr>
<tr class="t-nv"><td colspan="5"><div class="t-nv-ln-table"><div><a href="lock_tag_t.html" title="cpp/thread/lock tag t"><span class="t-lines"><span>defer_lock_t</span><span>try_to_lock_t</span><span>adopt_lock_t</span></span></a></div><div><span class="t-lines"><span><span class="t-mark-rev t-since-cxx11">(C++11)</span></span><span><span class="t-mark-rev t-since-cxx11">(C++11)</span></span><span><span class="t-mark-rev t-since-cxx11">(C++11)</span></span></span></div></div></td></tr>
</table></div></td><td><div><table class="t-nv-begin" cellpadding="0" style="">
<tr class="t-nv"><td colspan="5"><div class="t-nv-ln-table"><div><a href="lock.html" title="cpp/thread/lock"><span class="t-lines"><span>lock</span></span></a></div><div><span class="t-lines"><span><span class="t-mark-rev t-since-cxx11">(C++11)</span></span></span></div></div></td></tr>
<tr class="t-nv"><td colspan="5"><div class="t-nv-ln-table"><div><a href="try_lock.html" title="cpp/thread/try lock"><span class="t-lines"><span>try_lock</span></span></a></div><div><span class="t-lines"><span><span class="t-mark-rev t-since-cxx11">(C++11)</span></span></span></div></div></td></tr>
<tr class="t-nv"><td colspan="5"><div class="t-nv-ln-table"><div><a href="lock_tag.html" title="cpp/thread/lock tag"><span class="t-lines"><span>defer_lock</span><span>try_to_lock</span><span>adopt_lock</span></span></a></div><div><span class="t-lines"><span><span class="t-mark-rev t-since-cxx11">(C++11)</span></span><span><span class="t-mark-rev t-since-cxx11">(C++11)</span></span><span><span class="t-mark-rev t-since-cxx11">(C++11)</span></span></span></div></div></td></tr>
<tr class="t-nv"><td colspan="5"><div class="t-nv-ln-table"><div><a href="once_flag.html" title="cpp/thread/once flag"><span class="t-lines"><span>once_flag</span></span></a></div><div><span class="t-lines"><span><span class="t-mark-rev t-since-cxx11">(C++11)</span></span></span></div></div></td></tr>
<tr class="t-nv"><td colspan="5"><div class="t-nv-ln-table"><div><a href="call_once.html" title="cpp/thread/call once"><span class="t-lines"><span>call_once</span></span></a></div><div><span class="t-lines"><span><span class="t-mark-rev t-since-cxx11">(C++11)</span></span></span></div></div></td></tr>
</table></div></td></tr>
<tr class="t-nv-h1"><td colspan="5"> 条件变量</td></tr>
<tr class="t-nv"><td colspan="5"><div class="t-nv-ln-table"><div><a href="condition_variable.html" title="cpp/thread/condition variable"><span class="t-lines"><span>condition_variable</span></span></a></div><div><span class="t-lines"><span><span class="t-mark-rev t-since-cxx11">(C++11)</span></span></span></div></div></td></tr>
<tr class="t-nv"><td colspan="5"><div class="t-nv-ln-table"><div><a href="condition_variable_any.html" title="cpp/thread/condition variable any"><span class="t-lines"><span>condition_variable_any</span></span></a></div><div><span class="t-lines"><span><span class="t-mark-rev t-since-cxx11">(C++11)</span></span></span></div></div></td></tr>
<tr class="t-nv"><td colspan="5"><div class="t-nv-ln-table"><div><a href="notify_all_at_thread_exit.html" title="cpp/thread/notify all at thread exit"><span class="t-lines"><span>notify_all_at_thread_exit</span></span></a></div><div><span class="t-lines"><span><span class="t-mark-rev t-since-cxx11">(C++11)</span></span></span></div></div></td></tr>
<tr class="t-nv"><td colspan="5"><div class="t-nv-ln-table"><div><a href="cv_status.html" title="cpp/thread/cv status"><span class="t-lines"><span>cv_status</span></span></a></div><div><span class="t-lines"><span><span class="t-mark-rev t-since-cxx11">(C++11)</span></span></span></div></div></td></tr>
<tr class="t-nv-h1"><td colspan="5"> 信号量</td></tr>
<tr class="t-nv"><td colspan="5"><div class="t-nv-ln-table"><div><a href="counting_semaphore.html" title="cpp/thread/counting semaphore"><span class="t-lines"><span>counting_semaphore</span><span>binary_semaphore</span></span></a></div><div><span class="t-lines"><span><span class="t-mark-rev t-since-cxx20">(C++20)</span></span><span><span class="t-mark-rev t-since-cxx20">(C++20)</span></span></span></div></div></td></tr>
<tr class="t-nv-h1"><td colspan="5"> 闩与屏障</td></tr>
<tr class="t-nv"><td colspan="5"><div class="t-nv-ln-table"><div><a href="latch.html" title="cpp/thread/latch"><span class="t-lines"><span>latch</span></span></a></div><div><span class="t-lines"><span><span class="t-mark-rev t-since-cxx20">(C++20)</span></span></span></div></div></td></tr>
<tr class="t-nv"><td colspan="5"><div class="t-nv-ln-table"><div><a href="barrier.html" title="cpp/thread/barrier"><span class="t-lines"><span>barrier</span></span></a></div><div><span class="t-lines"><span><span class="t-mark-rev t-since-cxx20">(C++20)</span></span></span></div></div></td></tr>
<tr class="t-nv-h1"><td colspan="5"> future</td></tr>
<tr class="t-nv-col-table"><td><div><table class="t-nv-begin" cellpadding="0" style="">
<tr class="t-nv"><td colspan="5"><div class="t-nv-ln-table"><div><a href="promise.html" title="cpp/thread/promise"><span class="t-lines"><span>promise</span></span></a></div><div><span class="t-lines"><span><span class="t-mark-rev t-since-cxx11">(C++11)</span></span></span></div></div></td></tr>
<tr class="t-nv"><td colspan="5"><div class="t-nv-ln-table"><div><a href="future.html" title="cpp/thread/future"><span class="t-lines"><span>future</span></span></a></div><div><span class="t-lines"><span><span class="t-mark-rev t-since-cxx11">(C++11)</span></span></span></div></div></td></tr>
<tr class="t-nv"><td colspan="5"><div class="t-nv-ln-table"><div><a href="shared_future.html" title="cpp/thread/shared future"><span class="t-lines"><span>shared_future</span></span></a></div><div><span class="t-lines"><span><span class="t-mark-rev t-since-cxx11">(C++11)</span></span></span></div></div></td></tr>
<tr class="t-nv"><td colspan="5"><div class="t-nv-ln-table"><div><a href="packaged_task.html" title="cpp/thread/packaged task"><span class="t-lines"><span>packaged_task</span></span></a></div><div><span class="t-lines"><span><span class="t-mark-rev t-since-cxx11">(C++11)</span></span></span></div></div></td></tr>
<tr class="t-nv"><td colspan="5"><div class="t-nv-ln-table"><div><strong class="selflink"><span class="t-lines"><span>async</span></span></strong></div><div><span class="t-lines"><span><span class="t-mark-rev t-since-cxx11">(C++11)</span></span></span></div></div></td></tr>
</table></div></td><td><div><table class="t-nv-begin" cellpadding="0" style="">
<tr class="t-nv"><td colspan="5"><div class="t-nv-ln-table"><div><a href="launch.html" title="cpp/thread/launch"><span class="t-lines"><span>launch</span></span></a></div><div><span class="t-lines"><span><span class="t-mark-rev t-since-cxx11">(C++11)</span></span></span></div></div></td></tr>
<tr class="t-nv"><td colspan="5"><div class="t-nv-ln-table"><div><a href="future_status.html" title="cpp/thread/future status"><span class="t-lines"><span>future_status</span></span></a></div><div><span class="t-lines"><span><span class="t-mark-rev t-since-cxx11">(C++11)</span></span></span></div></div></td></tr>
<tr class="t-nv"><td colspan="5"><div class="t-nv-ln-table"><div><a href="future_error.html" title="cpp/thread/future error"><span class="t-lines"><span>future_error</span></span></a></div><div><span class="t-lines"><span><span class="t-mark-rev t-since-cxx11">(C++11)</span></span></span></div></div></td></tr>
<tr class="t-nv"><td colspan="5"><div class="t-nv-ln-table"><div><a href="future/future_category.html" title="cpp/thread/future category"><span class="t-lines"><span>future_category</span></span></a></div><div><span class="t-lines"><span><span class="t-mark-rev t-since-cxx11">(C++11)</span></span></span></div></div></td></tr>
<tr class="t-nv"><td colspan="5"><div class="t-nv-ln-table"><div><a href="future_errc.html" title="cpp/thread/future errc"><span class="t-lines"><span>future_errc</span></span></a></div><div><span class="t-lines"><span><span class="t-mark-rev t-since-cxx11">(C++11)</span></span></span></div></div></td></tr>
</table></div></td></tr>
</table></div><div></div></div></div></div><div class="t-navbar-sep"> </div></div>
<table class="t-dcl-begin"><tbody>
<tr class="t-dsc-header">
<td> <div>定义于头文件 <code><a href="../header/future.html" title="cpp/header/future">&lt;future&gt;</a></code>
 </div></td>
<td></td>
<td></td>
</tr>
</tbody>
<tbody class="t-dcl-rev t-dcl-rev-num"><tr class="t-dcl-rev-aux">
<td></td>
<td rowspan="142">(1)</td>
<td></td>
</tr>
<tr class="t-dcl t-since-cxx11 t-until-cxx17">
<td> <div><span class="mw-geshi cpp source-cpp"><span class="kw1">template</span><span class="sy1">&lt;</span> <span class="kw1">class</span> Function, <span class="kw1">class</span>... <span class="me1">Args</span><span class="sy1">&gt;</span><br>
<p><a href="future.html"><span class="kw2180">std::<span class="me2">future</span></span></a><span class="sy1">&lt;</span><a href="../types/result_of.html"><span class="kw640">std::<span class="me2">result_of_t</span></span></a><span class="sy1">&lt;</span><a href="../types/decay.html"><span class="kw629">std::<span class="me2">decay_t</span></span></a><span class="sy1">&lt;</span>Function<span class="sy1">&gt;</span><span class="br0">(</span><a href="../types/decay.html"><span class="kw629">std::<span class="me2">decay_t</span></span></a><span class="sy1">&lt;</span>Args<span class="sy1">&gt;</span>...<span class="br0">)</span><span class="sy1">&gt;&gt;</span><br>
</p>
    async<span class="br0">(</span> Function<span class="sy3">&amp;&amp;</span> f, Args<span class="sy3">&amp;&amp;</span>... <span class="me1">args</span> <span class="br0">)</span><span class="sy4">;</span></span></div></td>
<td class="t-dcl-nopad">  </td>
<td> <span class="t-mark-rev t-since-cxx11">(C++11 起)</span> <br><span class="t-mark-rev t-until-cxx17">(C++17 前)</span> </td>
</tr>
<tr class="t-dcl t-since-cxx17 t-until-cxx20">
<td> <div><span class="mw-geshi cpp source-cpp"><span class="kw1">template</span><span class="sy1">&lt;</span> <span class="kw1">class</span> Function, <span class="kw1">class</span>... <span class="me1">Args</span><span class="sy1">&gt;</span><br>
<p><a href="future.html"><span class="kw2180">std::<span class="me2">future</span></span></a><span class="sy1">&lt;</span>std<span class="sy4">::</span><span class="me2">invoke_result_t</span><span class="sy1">&lt;</span><a href="../types/decay.html"><span class="kw629">std::<span class="me2">decay_t</span></span></a><span class="sy1">&lt;</span>Function<span class="sy1">&gt;</span>, <br>
                                              <a href="../types/decay.html"><span class="kw629">std::<span class="me2">decay_t</span></span></a><span class="sy1">&lt;</span>Args<span class="sy1">&gt;</span>...<span class="sy1">&gt;&gt;</span><br>
</p>
    async<span class="br0">(</span> Function<span class="sy3">&amp;&amp;</span> f, Args<span class="sy3">&amp;&amp;</span>... <span class="me1">args</span> <span class="br0">)</span><span class="sy4">;</span></span></div></td>
<td class="t-dcl-nopad">  </td>
<td> <span class="t-mark-rev t-since-cxx17">(C++17 起)</span> <br><span class="t-mark-rev t-until-cxx20">(C++20 前)</span> </td>
</tr>
<tr class="t-dcl t-since-cxx20">
<td> <div><span class="mw-geshi cpp source-cpp"><span class="kw1">template</span><span class="sy1">&lt;</span> <span class="kw1">class</span> Function, <span class="kw1">class</span>... <span class="me1">Args</span><span class="sy1">&gt;</span><br>
<p><span class="br0">[</span><span class="br0">[</span>nodiscard<span class="br0">]</span><span class="br0">]</span> <br>
<a href="future.html"><span class="kw2180">std::<span class="me2">future</span></span></a><span class="sy1">&lt;</span>std<span class="sy4">::</span><span class="me2">invoke_result_t</span><span class="sy1">&lt;</span><a href="../types/decay.html"><span class="kw629">std::<span class="me2">decay_t</span></span></a><span class="sy1">&lt;</span>Function<span class="sy1">&gt;</span>, <br>
                                 <a href="../types/decay.html"><span class="kw629">std::<span class="me2">decay_t</span></span></a><span class="sy1">&lt;</span>Args<span class="sy1">&gt;</span>...<span class="sy1">&gt;&gt;</span><br>
</p>
    async<span class="br0">(</span> Function<span class="sy3">&amp;&amp;</span> f, Args<span class="sy3">&amp;&amp;</span>... <span class="me1">args</span> <span class="br0">)</span><span class="sy4">;</span></span></div></td>
<td class="t-dcl-nopad">  </td>
<td> <span class="t-mark-rev t-since-cxx20">(C++20 起)</span> </td>
</tr>
</tbody><tbody>

</tbody>
<tbody class="t-dcl-rev t-dcl-rev-num"><tr class="t-dcl-rev-aux">
<td></td>
<td rowspan="142">(2)</td>
<td></td>
</tr>
<tr class="t-dcl t-since-cxx11 t-until-cxx17">
<td> <div><span class="mw-geshi cpp source-cpp"><span class="kw1">template</span><span class="sy1">&lt;</span> <span class="kw1">class</span> Function, <span class="kw1">class</span>... <span class="me1">Args</span> <span class="sy1">&gt;</span><br>
<p><a href="future.html"><span class="kw2180">std::<span class="me2">future</span></span></a><span class="sy1">&lt;</span><a href="../types/result_of.html"><span class="kw640">std::<span class="me2">result_of_t</span></span></a><span class="sy1">&lt;</span><a href="../types/decay.html"><span class="kw629">std::<span class="me2">decay_t</span></span></a><span class="sy1">&lt;</span>Function<span class="sy1">&gt;</span><span class="br0">(</span><a href="../types/decay.html"><span class="kw629">std::<span class="me2">decay_t</span></span></a><span class="sy1">&lt;</span>Args<span class="sy1">&gt;</span>...<span class="br0">)</span><span class="sy1">&gt;&gt;</span><br>
</p>
    async<span class="br0">(</span> <a href="launch.html"><span class="kw2184">std::<span class="me2">launch</span></span></a> policy, Function<span class="sy3">&amp;&amp;</span> f, Args<span class="sy3">&amp;&amp;</span>... <span class="me1">args</span> <span class="br0">)</span><span class="sy4">;</span></span></div></td>
<td class="t-dcl-nopad">  </td>
<td> <span class="t-mark-rev t-since-cxx11">(C++11 起)</span> <br><span class="t-mark-rev t-until-cxx17">(C++17 前)</span> </td>
</tr>
<tr class="t-dcl t-since-cxx17 t-until-cxx20">
<td> <div><span class="mw-geshi cpp source-cpp"><span class="kw1">template</span><span class="sy1">&lt;</span> <span class="kw1">class</span> Function, <span class="kw1">class</span>... <span class="me1">Args</span> <span class="sy1">&gt;</span><br>
<p><a href="future.html"><span class="kw2180">std::<span class="me2">future</span></span></a><span class="sy1">&lt;</span>std<span class="sy4">::</span><span class="me2">invoke_result_t</span><span class="sy1">&lt;</span><a href="../types/decay.html"><span class="kw629">std::<span class="me2">decay_t</span></span></a><span class="sy1">&lt;</span>Function<span class="sy1">&gt;</span>, <br>
                                 <a href="../types/decay.html"><span class="kw629">std::<span class="me2">decay_t</span></span></a><span class="sy1">&lt;</span>Args<span class="sy1">&gt;</span>...<span class="sy1">&gt;&gt;</span><br>
</p>
    async<span class="br0">(</span> <a href="launch.html"><span class="kw2184">std::<span class="me2">launch</span></span></a> policy, Function<span class="sy3">&amp;&amp;</span> f, Args<span class="sy3">&amp;&amp;</span>... <span class="me1">args</span> <span class="br0">)</span><span class="sy4">;</span></span></div></td>
<td class="t-dcl-nopad">  </td>
<td> <span class="t-mark-rev t-since-cxx17">(C++17 起)</span> <br><span class="t-mark-rev t-until-cxx20">(C++20 前)</span> </td>
</tr>
<tr class="t-dcl t-since-cxx20">
<td> <div><span class="mw-geshi cpp source-cpp"><span class="kw1">template</span><span class="sy1">&lt;</span> <span class="kw1">class</span> Function, <span class="kw1">class</span>... <span class="me1">Args</span> <span class="sy1">&gt;</span><br>
<p><span class="br0">[</span><span class="br0">[</span>nodiscard<span class="br0">]</span><span class="br0">]</span> <br>
<a href="future.html"><span class="kw2180">std::<span class="me2">future</span></span></a><span class="sy1">&lt;</span>std<span class="sy4">::</span><span class="me2">invoke_result_t</span><span class="sy1">&lt;</span><a href="../types/decay.html"><span class="kw629">std::<span class="me2">decay_t</span></span></a><span class="sy1">&lt;</span>Function<span class="sy1">&gt;</span>, <br>
                                 <a href="../types/decay.html"><span class="kw629">std::<span class="me2">decay_t</span></span></a><span class="sy1">&lt;</span>Args<span class="sy1">&gt;</span>...<span class="sy1">&gt;&gt;</span><br>
</p>
    async<span class="br0">(</span> <a href="launch.html"><span class="kw2184">std::<span class="me2">launch</span></span></a> policy, Function<span class="sy3">&amp;&amp;</span> f, Args<span class="sy3">&amp;&amp;</span>... <span class="me1">args</span> <span class="br0">)</span><span class="sy4">;</span></span></div></td>
<td class="t-dcl-nopad">  </td>
<td> <span class="t-mark-rev t-since-cxx20">(C++20 起)</span> </td>
</tr>
</tbody><tbody>
<tr class="t-dcl-sep"><td></td><td></td><td></td></tr>
</tbody></table>
<p>函数模板 <code>async</code> 异步地运行函数 <code>f</code> （潜在地在可能是线程池一部分的分离线程中），并返回最终将保有该函数调用结果的 <span class="t-lc"><a href="future.html" title="cpp/thread/future">std::future</a></span> 。
</p>
<div class="t-li1"><span class="t-li">1)</span> 表现如同以 <span class="t-c"><span class="mw-geshi cpp source-cpp">policy</span></span> 为 <span class="t-c"><span class="mw-geshi cpp source-cpp"><a href="launch.html"><span class="kw2185">std::<span class="me2">launch</span><span class="sy4">::</span><span class="me2">async</span></span></a> <span class="sy3">|</span> <a href="launch.html"><span class="kw2186">std::<span class="me2">launch</span><span class="sy4">::</span><span class="me2">deferred</span></span></a></span></span> 调用 <span class="t-v">(2)</span> 。换言之， <code>f</code> 可能执行于另一线程，或者它可能在查询产生的 <span class="t-lc"><a href="future.html" title="cpp/thread/future">std::future</a></span> 的值时同步运行。</div>
<div class="t-li1"><span class="t-li">2)</span> 按照特定的执行策略 <code>policy</code> ，以参数 <code>args</code> 调用函数 <code>f</code> ：
<dl><dd><ul><li> 若设置 <i>async</i> 标志（即 <span class="t-c"><span class="mw-geshi cpp source-cpp"><span class="br0">(</span>policy <span class="sy3">&amp;</span> <a href="launch.html"><span class="kw2185">std::<span class="me2">launch</span><span class="sy4">::</span><span class="me2">async</span></span></a><span class="br0">)</span> <span class="sy3">!</span><span class="sy1">=</span> <span class="nu0">0</span></span></span> ），则 <code>async</code> 在新的执行线程（初始化所有线程局域对象后）执行可调用对象 <code>f</code> ，如同产出 <span class="t-c"><span class="mw-geshi cpp source-cpp"><a href="thread.html"><span class="kw2146">std::<span class="me2">thread</span></span></a><span class="br0">(</span><a href="../utility/forward.html"><span class="kw1117">std::<span class="me2">forward</span></span></a><span class="sy1">&lt;</span>F<span class="sy1">&gt;</span><span class="br0">(</span>f<span class="br0">)</span>, <a href="../utility/forward.html"><span class="kw1117">std::<span class="me2">forward</span></span></a><span class="sy1">&lt;</span>Args<span class="sy1">&gt;</span><span class="br0">(</span>args<span class="br0">)</span>...<span class="br0">)</span></span></span> ，除了若 <code>f</code> 返回值或抛出异常，则于可通过 <code>async</code> 返回给调用方的 <span class="t-lc"><a href="future.html" title="cpp/thread/future">std::future</a></span> 访问的共享状态存储结果。
</li><li> 若设置 <i>deferred</i> 标志（即 <span class="t-c"><span class="mw-geshi cpp source-cpp"><span class="br0">(</span>policy <span class="sy3">&amp;</span> <a href="launch.html"><span class="kw2186">std::<span class="me2">launch</span><span class="sy4">::</span><span class="me2">deferred</span></span></a><span class="br0">)</span> <span class="sy3">!</span><span class="sy1">=</span> <span class="nu0">0</span></span></span> ），则 <code>async</code> 以同 <span class="t-lc"><a href="thread.html" title="cpp/thread/thread">std::thread</a></span> 构造函数的方式转换 <code>f</code> 与 <code>args...</code> ，但不产出新的执行线程。而是进行<i>惰性求值</i>：在 <code>async</code> 所返回的 <span class="t-lc"><a href="future.html" title="cpp/thread/future">std::future</a></span> 上首次调用非定时等待函数，将导致在当前线程（不必是最初调用 <code>std::async</code> 的线程）中，以 <code>args...</code> （作为右值传递）的副本调用 <code>f</code> （亦作为右值）的副本。将结果或异常置于关联到该 future 的共享状态，然后才令它就绪。对同一 <span class="t-lc"><a href="future.html" title="cpp/thread/future">std::future</a></span> 的所有后续访问都会立即返回结果。
</li><li> 若 <code>policy</code> 中设置了 <span class="t-lc"><a href="launch.html" title="cpp/thread/launch">std::launch::async</a></span> 和 <span class="t-lc"><a href="launch.html" title="cpp/thread/launch">std::launch::deferred</a></span> 两个标志，则进行异步执行还是惰性求值取决于实现。
</li></ul>
</dd></dl>
 <table class="t-rev-begin">
<tr class="t-rev t-since-cxx14"><td>
<dl><dd><ul><li> 若 <code>policy</code> 中未设置 <span class="t-lc"><a href="launch.html" title="cpp/thread/launch">std::launch::async</a></span> 或 <span class="t-lc"><a href="launch.html" title="cpp/thread/launch">std::launch::deferred</a></span> 或任何实现定义策略标志，则行为未定义。
</li></ul>
</dd></dl>
</td>
<td><span class="t-mark-rev t-since-cxx14">(C++14 起)</span></td></tr>
</table></div>
<p>任何情况下，对 <code>std::async</code> 的调用<i>同步于</i>（定义于 <span class="t-lc"><a href="../atomic/memory_order.html" title="cpp/atomic/memory order">std::memory_order</a></span> ）对 <code>f</code> 的调用，且 <code>f</code> 的完成<i>先序于</i>令共享状态就绪。若选择 <code>async</code> 策略，则关联线程的完成<i>同步于</i>首个等待于共享状态上的函数的成功返回，或最后一个释放共享状态的函数的返回，两者的先到来者。
</p>
<h3><span class="mw-headline" id=".E5.8F.82.E6.95.B0">参数</span></h3>
<table class="t-par-begin">


<tr class="t-par">
<td>  f
</td>
<td> -
</td>
<td> 要调用的<a href="../named_req/Callable.html" title="cpp/named req/Callable"><i>可调用</i> <span style="font-family: Georgia, 'DejaVu Serif', serif; font-style:italic">(Callable)</span> </a>对象
</td></tr>
<tr class="t-par">
<td>  args...
</td>
<td> -
</td>
<td> 传递给 <code>f</code> 的参数
</td></tr>
<tr class="t-par">
<td>  policy
</td>
<td> -
</td>
<td> 位掩码值，每个单独位控制允许的执行方法
<table class="t-dsc-begin">

<tr class="t-dsc-sep">
<td colspan="2">  
</td></tr>
<tr class="t-dsc-hitem">
<td> 位
</td>
<td> 解释
</td></tr>


<tr class="t-dsc">
<td>  <span class="t-lc"><a href="launch.html" title="cpp/thread/launch">std::launch::async</a></span>
</td>
<td> 启用异步求值
</td></tr>

<tr class="t-dsc">
<td>  <span class="t-lc"><a href="launch.html" title="cpp/thread/launch">std::launch::deferred</a></span>
</td>
<td> 启用惰性求值
</td></tr>
</table>
<p><br>
</p>
</td></tr>
<tr class="t-par-hitem">
<td colspan="3"> 类型要求
</td></tr>
<tr class="t-par-req">
<td colspan="3"> -<div class="spacer"></div><code>Function, Args</code> 必须满足<a href="../named_req/MoveConstructible.html" title="cpp/named req/MoveConstructible"><i>可移动构造</i> <span style="font-family: Georgia, 'DejaVu Serif', serif; font-style:italic">(MoveConstructible)</span> </a> 的要求。
</td></tr></table>
<h3><span class="mw-headline" id=".E8.BF.94.E5.9B.9E.E5.80.BC">返回值</span></h3>
<p>指代此次调用 <code>std::async</code> 所创建的共享状态的 <span class="t-lc"><a href="future.html" title="cpp/thread/future">std::future</a></span> 。
</p>
<h3><span class="mw-headline" id=".E5.BC.82.E5.B8.B8">异常</span></h3>
<p>若运行策略等于 <span class="t-lc"><a href="launch.html" title="cpp/thread/launch">std::launch::async</a></span> 且实现无法开始新线程（该情况下，若运行策略为 <code>async|deferred</code> 或设置了额外位，则它将回退到 deferred 或实现定义的策略），则抛出以 <span class="t-lc"><a href="../error/errc.html" title="cpp/error/errc">std::errc::resource_unavailable_try_again</a></span> 为错误条件的 <span class="t-lc"><a href="../error/system_error.html" title="cpp/error/system error">std::system_error</a></span> ，或者若无法分配内部数据结构所用的内存，则为 <span class="t-lc"><a href="../memory/new/bad_alloc.html" title="cpp/memory/new/bad alloc">std::bad_alloc</a></span> 。
</p>
<h3><span class="mw-headline" id=".E6.B3.A8.E8.A7.A3">注解</span></h3>
<p>实现可以通过在默认运行策略中启用额外（实现定义的）位，扩展 <span class="t-lc"><strong class="selflink">std::async</strong></span> 第一重载的行为。
</p><p>实现定义的运行策略的例子是同步策略（在 async 调用内立即执行）和任务策略（类似 async ，但不清理线程局域对象）。
</p><p>若从 <code>std::async</code> 获得的 <code>std::future</code> 未被移动或绑定到引用，则在完整表达式结尾， <span class="t-lc"><a href="future.html" title="cpp/thread/future">std::future</a></span> 的析构函数将阻塞直至异步计算完成，实质上令如下代码同步：
</p>
<div dir="ltr" class="mw-geshi" style="text-align: left;"><div class="cpp source-cpp"><pre class="de1">std<span class="sy4">::</span><span class="me2">async</span><span class="br0">(</span><a href="launch.html"><span class="kw2185">std::<span class="me2">launch</span><span class="sy4">::</span><span class="me2">async</span></span></a>, <span class="br0">[</span><span class="br0">]</span><span class="br0">{</span> f<span class="br0">(</span><span class="br0">)</span><span class="sy4">;</span> <span class="br0">}</span><span class="br0">)</span><span class="sy4">;</span> <span class="co1">// 临时量的析构函数等待 f()</span>
std<span class="sy4">::</span><span class="me2">async</span><span class="br0">(</span><a href="launch.html"><span class="kw2185">std::<span class="me2">launch</span><span class="sy4">::</span><span class="me2">async</span></span></a>, <span class="br0">[</span><span class="br0">]</span><span class="br0">{</span> g<span class="br0">(</span><span class="br0">)</span><span class="sy4">;</span> <span class="br0">}</span><span class="br0">)</span><span class="sy4">;</span> <span class="co1">// f() 完成前不开始</span></pre></div></div>
<p>（注意，以调用 std::async 以外的方式获得的 std::future 的析构函数决不阻塞）
</p>
<h3><span class="mw-headline" id=".E7.A4.BA.E4.BE.8B">示例</span></h3>
<div class="t-example"><div class="t-example-live-link"><div class="coliru-btn coliru-btn-run-init">运行此代码</div></div>
<div dir="ltr" class="mw-geshi" style="text-align: left;"><div class="cpp source-cpp"><pre class="de1"><span class="co2">#include &lt;iostream&gt;</span>
<span class="co2">#include &lt;vector&gt;</span>
<span class="co2">#include &lt;algorithm&gt;</span>
<span class="co2">#include &lt;numeric&gt;</span>
<span class="co2">#include &lt;future&gt;</span>
<span class="co2">#include &lt;string&gt;</span>
<span class="co2">#include &lt;mutex&gt;</span>
 
<a href="mutex.html"><span class="kw2154">std::<span class="me2">mutex</span></span></a> m<span class="sy4">;</span>
<span class="kw1">struct</span> X <span class="br0">{</span>
    <span class="kw4">void</span> foo<span class="br0">(</span><span class="kw4">int</span> i, <span class="kw4">const</span> <a href="../string/basic_string.html"><span class="kw1230">std::<span class="me2">string</span></span></a><span class="sy3">&amp;</span> str<span class="br0">)</span> <span class="br0">{</span>
        <a href="lock_guard.html"><span class="kw2166">std::<span class="me2">lock_guard</span></span></a><span class="sy1">&lt;</span><a href="mutex.html"><span class="kw2154">std::<span class="me2">mutex</span></span></a><span class="sy1">&gt;</span> lk<span class="br0">(</span>m<span class="br0">)</span><span class="sy4">;</span>
        <a href="../io/cout.html"><span class="kw1758">std::<span class="me2">cout</span></span></a> <span class="sy1">&lt;&lt;</span> str <span class="sy1">&lt;&lt;</span> <span class="st0">' '</span> <span class="sy1">&lt;&lt;</span> i <span class="sy1">&lt;&lt;</span> <span class="st0">'<span class="es1">\n</span>'</span><span class="sy4">;</span>
    <span class="br0">}</span>
    <span class="kw4">void</span> bar<span class="br0">(</span><span class="kw4">const</span> <a href="../string/basic_string.html"><span class="kw1230">std::<span class="me2">string</span></span></a><span class="sy3">&amp;</span> str<span class="br0">)</span> <span class="br0">{</span>
        <a href="lock_guard.html"><span class="kw2166">std::<span class="me2">lock_guard</span></span></a><span class="sy1">&lt;</span><a href="mutex.html"><span class="kw2154">std::<span class="me2">mutex</span></span></a><span class="sy1">&gt;</span> lk<span class="br0">(</span>m<span class="br0">)</span><span class="sy4">;</span>
        <a href="../io/cout.html"><span class="kw1758">std::<span class="me2">cout</span></span></a> <span class="sy1">&lt;&lt;</span> str <span class="sy1">&lt;&lt;</span> <span class="st0">'<span class="es1">\n</span>'</span><span class="sy4">;</span>
    <span class="br0">}</span>
    <span class="kw4">int</span> operator<span class="br0">(</span><span class="br0">)</span><span class="br0">(</span><span class="kw4">int</span> i<span class="br0">)</span> <span class="br0">{</span>
        <a href="lock_guard.html"><span class="kw2166">std::<span class="me2">lock_guard</span></span></a><span class="sy1">&lt;</span><a href="mutex.html"><span class="kw2154">std::<span class="me2">mutex</span></span></a><span class="sy1">&gt;</span> lk<span class="br0">(</span>m<span class="br0">)</span><span class="sy4">;</span>
        <a href="../io/cout.html"><span class="kw1758">std::<span class="me2">cout</span></span></a> <span class="sy1">&lt;&lt;</span> i <span class="sy1">&lt;&lt;</span> <span class="st0">'<span class="es1">\n</span>'</span><span class="sy4">;</span>
        <span class="kw1">return</span> i <span class="sy2">+</span> <span class="nu0">10</span><span class="sy4">;</span>
    <span class="br0">}</span>
<span class="br0">}</span><span class="sy4">;</span>
 
<span class="kw1">template</span> <span class="sy1">&lt;</span><span class="kw1">typename</span> RandomIt<span class="sy1">&gt;</span>
<span class="kw4">int</span> parallel_sum<span class="br0">(</span>RandomIt beg, RandomIt end<span class="br0">)</span>
<span class="br0">{</span>
    <span class="kw4">auto</span> len <span class="sy1">=</span> end <span class="sy2">-</span> beg<span class="sy4">;</span>
    <span class="kw1">if</span> <span class="br0">(</span>len <span class="sy1">&lt;</span> <span class="nu0">1000</span><span class="br0">)</span>
        <span class="kw1">return</span> <a href="http://zh.cppreference.com/w/cpp/algorithm/accumulate"><span class="kw1651">std::<span class="me2">accumulate</span></span></a><span class="br0">(</span>beg, end, <span class="nu0">0</span><span class="br0">)</span><span class="sy4">;</span>
 
    RandomIt mid <span class="sy1">=</span> beg <span class="sy2">+</span> len<span class="sy2">/</span><span class="nu0">2</span><span class="sy4">;</span>
    <span class="kw4">auto</span> handle <span class="sy1">=</span> std<span class="sy4">::</span><span class="me2">async</span><span class="br0">(</span><a href="launch.html"><span class="kw2185">std::<span class="me2">launch</span><span class="sy4">::</span><span class="me2">async</span></span></a>,
                             parallel_sum<span class="sy1">&lt;</span>RandomIt<span class="sy1">&gt;</span>, mid, end<span class="br0">)</span><span class="sy4">;</span>
    <span class="kw4">int</span> sum <span class="sy1">=</span> parallel_sum<span class="br0">(</span>beg, mid<span class="br0">)</span><span class="sy4">;</span>
    <span class="kw1">return</span> sum <span class="sy2">+</span> handle.<span class="me1">get</span><span class="br0">(</span><span class="br0">)</span><span class="sy4">;</span>
<span class="br0">}</span>
 
<span class="kw4">int</span> main<span class="br0">(</span><span class="br0">)</span>
<span class="br0">{</span>
    <a href="../container/vector.html"><span class="kw1269">std::<span class="me2">vector</span></span></a><span class="sy1">&lt;</span><span class="kw4">int</span><span class="sy1">&gt;</span> v<span class="br0">(</span><span class="nu0">10000</span>, <span class="nu0">1</span><span class="br0">)</span><span class="sy4">;</span>
    <a href="../io/cout.html"><span class="kw1758">std::<span class="me2">cout</span></span></a> <span class="sy1">&lt;&lt;</span> <span class="st0">"The sum is "</span> <span class="sy1">&lt;&lt;</span> parallel_sum<span class="br0">(</span>v.<span class="me1">begin</span><span class="br0">(</span><span class="br0">)</span>, v.<span class="me1">end</span><span class="br0">(</span><span class="br0">)</span><span class="br0">)</span> <span class="sy1">&lt;&lt;</span> <span class="st0">'<span class="es1">\n</span>'</span><span class="sy4">;</span>
 
    X x<span class="sy4">;</span>
    <span class="co1">// 以默认策略调用 x.foo(42, "Hello") ：</span>
    <span class="co1">// 可能同时打印 "Hello 42" 或延迟执行</span>
    <span class="kw4">auto</span> a1 <span class="sy1">=</span> std<span class="sy4">::</span><span class="me2">async</span><span class="br0">(</span><span class="sy3">&amp;</span>X<span class="sy4">::</span><span class="me2">foo</span>, <span class="sy3">&amp;</span>x, <span class="nu0">42</span>, <span class="st0">"Hello"</span><span class="br0">)</span><span class="sy4">;</span>
    <span class="co1">// 以 deferred 策略调用 x.bar("world!")</span>
    <span class="co1">// 调用 a2.get() 或 a2.wait() 时打印 "world!"</span>
    <span class="kw4">auto</span> a2 <span class="sy1">=</span> std<span class="sy4">::</span><span class="me2">async</span><span class="br0">(</span><a href="launch.html"><span class="kw2186">std::<span class="me2">launch</span><span class="sy4">::</span><span class="me2">deferred</span></span></a>, <span class="sy3">&amp;</span>X<span class="sy4">::</span><span class="me2">bar</span>, x, <span class="st0">"world!"</span><span class="br0">)</span><span class="sy4">;</span>
    <span class="co1">// 以 async 策略调用 X()(43) ：</span>
    <span class="co1">// 同时打印 "43"</span>
    <span class="kw4">auto</span> a3 <span class="sy1">=</span> std<span class="sy4">::</span><span class="me2">async</span><span class="br0">(</span><a href="launch.html"><span class="kw2185">std::<span class="me2">launch</span><span class="sy4">::</span><span class="me2">async</span></span></a>, X<span class="br0">(</span><span class="br0">)</span>, <span class="nu0">43</span><span class="br0">)</span><span class="sy4">;</span>
    a2.<span class="me1">wait</span><span class="br0">(</span><span class="br0">)</span><span class="sy4">;</span>                     <span class="co1">// 打印 "world!"</span>
    <a href="../io/cout.html"><span class="kw1758">std::<span class="me2">cout</span></span></a> <span class="sy1">&lt;&lt;</span> a3.<span class="me1">get</span><span class="br0">(</span><span class="br0">)</span> <span class="sy1">&lt;&lt;</span> <span class="st0">'<span class="es1">\n</span>'</span><span class="sy4">;</span> <span class="co1">// 打印 "53"</span>
<span class="br0">}</span> <span class="co1">// 若 a1 在此点未完成，则 a1 的析构函数在此打印 "Hello 42"</span></pre></div></div>
<p>可能的输出：
</p>
<div dir="ltr" class="mw-geshi" style="text-align: left;"><div class="text source-text"><pre class="de1">The sum is 10000
43
world!
53
Hello 42</pre></div></div> 
</div>
<h3><span class="mw-headline" id=".E7.BC.BA.E9.99.B7.E6.8A.A5.E5.91.8A">缺陷报告</span></h3>
<p>下列更改行为的缺陷报告追溯地应用于以前出版的 C++ 标准。
</p>
<table class="dsctable" style="font-size:0.8em">
<tr>
<th> DR
</th>
<th> 应用于
</th>
<th> 出版时的行为
</th>
<th> 正确行为
</th></tr>
<tr>
<td> <a rel="nofollow" class="external text" href="https://cplusplus.github.io/LWG/issue2021">LWG 2021</a>
</td>
<td> C++11
</td>
<td> 返回类型不正确， deferred 的情况下参数的值类别不明
</td>
<td> 更正返回类型，明确使用右值
</td></tr></table>

<!-- 
NewPP limit report
Preprocessor visited node count: 6101/1000000
Preprocessor generated node count: 11700/1000000
Post‐expand include size: 156217/2097152 bytes
Template argument size: 38764/2097152 bytes
Highest expansion depth: 25/40
Expensive parser function count: 0/100
-->

<!-- Saved in parser cache with key mwiki1-mwiki_zh_:pcache:idhash:3740-0!*!0!!zh!*!zh!* and timestamp 20200307164734 -->
</div>                    <!-- /bodycontent -->
                                        <!-- printfooter -->
                    <div class="printfooter">
                    来自“<a href="https://zh.cppreference.com/mwiki/index.php?title=cpp/thread/async&amp;oldid=65236">https://zh.cppreference.com/mwiki/index.php?title=cpp/thread/async&amp;oldid=65236</a>”                    </div>
                    <!-- /printfooter -->
                                                            <!-- catlinks -->
                    <!-- /catlinks -->
                                                            <div class="visualClear"></div>
                    <!-- debughtml -->
                                        <!-- /debughtml -->
                </div>
                <!-- /bodyContent -->
            </div>
        </div>
        <!-- /content -->
        <!-- footer -->
        <div id="cpp-footer-base" class="noprint">
            <div id="footer">
                        <div id="cpp-navigation">
            <h5>导航</h5>
            <ul><li><a href="https://zh.cppreference.com/w/cpp/thread/async">Online version</a></li><li>Offline version retrieved 2020-03-14 18:00.</li></ul></div>
                        <ul id="footer-info">
                                    <li id="footer-info-lastmod"> 本页面最后修改于2020年3月7日 (星期六) 08:47。</li>
                                    <li id="footer-info-viewcount">此页面已被浏览过23,271次。</li>
                            </ul>
                    </div>
        </div>
        <!-- /footer -->
        <script>if(window.mw){
mw.loader.state({"site":"loading","user":"missing","user.groups":"ready"});
}</script>
<script src="../../../common/skin_scripts.js"></script>
<script>if(window.mw){
mw.loader.load(["mediawiki.action.view.postEdit","mediawiki.user","mediawiki.page.ready","mediawiki.searchSuggest","mediawiki.hidpi","ext.gadget.MathJax","ext.gadget.ColiruCompiler"], null, true);
}</script>
<script src="../../../common/site_scripts.js"></script>
<script type="text/javascript">
var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'UA-2828341-1']);
_gaq.push(['_setDomainName', 'cppreference.com']);
_gaq.push(['_trackPageview']);
</script><!-- Served in 0.098 secs. -->
	</body>
<!-- Cached 20200307203512 -->
</html>